#!/usr/bin/env python

import subprocess
import urllib2
import json 

def getShortUrl(_url):
	req = urllib2.Request('https://www.googleapis.com/urlshortener/v1/url')
	req.add_header("Content-Type","application/json")
	req.add_data('{"longUrl": "'+_url+'"}')
	res = urllib2.urlopen(req)

	result = json.load(res)

	for key,value in result.iteritems():
		print key+"\t\t"+value
		

def getLongUrl(_url):
	request = urllib2.Request('https://www.googleapis.com/urlshortener/v1/url?shortUrl='+_url)	
	res = urllib2.urlopen(request)

        result = json.load(res)

        for key,value in result.iteritems():
                print key+"\t\t"+value


	
ctrl = ''

while True:
	
	ctrl = raw_input("[1] to shorten\n[2] to lengthen\n:")

	if ctrl == '1':
		url_to_shorten = raw_input("Enter a url to shorten:")
		getShortUrl(url_to_shorten)

	elif ctrl == '2':
		url_to_lengthen = raw_input("Enter a url to expand:")
		getLongUrl(url_to_lengthen)
	
	elif ctrl == 'q':
		break
	
	else:
		print "Please enter a valid selection"




